
import datetime

from google.appengine.ext import db

#App imports
import models
import budget
import appuser

def GetBudgetYears():

  user = appuser.GetUser()
  html = ''
  now = datetime.datetime.now()
  nowyear = int(now.year)
  Flag = False
  budget = models.BudgetExpenseByCategory.gql("WHERE User = :1 ORDER BY Year DESC", user.key())
  html += '<select id="ddlBudgetYear" name="ddlBudgetYear" class="ddl">\r\n'
  for b in budget:
    if nowyear == b.Year:
      Flag = True 
    html += '<option value="' + str(b.Year) + '">' + str(b.Year) + '</option>\r\n'

  if Flag == False:
    html += '<option value="' + str(nowyear) + '">' + str(nowyear) + '</option>\r\n'
  html += '</select>'

  return html

def GetBudgetExpenseCategoriesNotUsed(pSelectedCategories):
  """This gets all the expense categories which have not been configured."""
  html = ''
  html += '<select id="ddlNewExpCategory" name="ddlNewExpCategory" class="ddl" onchange="doSelectCategory(this.value);">\r\n'
  html += '<option value="">Add Category...</option>\r\n'
  categories = models.Category.gql("WHERE Active = True ORDER BY CategoryName ASC")
  for category in categories:
    if category.CategoryName.lower() not in pSelectedCategories:
      html += '<option value="' + category.CategoryName  + '">' + category.CategoryName  + '</option>\r\n'

  html += '<option value="">--------------------------</option>\r\n'
  html += '<option value="new">New Category...</option>\r\n' 
  html += '</select>'

  return html

def GetBudgetByCategoriesActionButtons(pId):
  """This gets Budget by categories action buttons."""
  html = '<div class="divAction">\r\n'
  html += '<input id="btnSaveBudget' + pId + '" name="btnSaveBudget' + pId + '" class="button" type="button" value="Save Budget" onclick="doSaveBudget()"  />\r\n'
  html += '<span style="padding-left:10px;">&nbsp;</span>\r\n'
  html += '<input id="btnLockBudget' + pId + '" name="btnLockBudget' + pId + '" class="button" type="button" value="Lock Budget" onclick="doLockBudget()"  />\r\n'
  html += '</div>\r\n'
  return html

def GetBudgetExpenseByCategories(pSelectedYear):
  user = appuser.GetUser()
   
  html =  '<div class="Label">Please enter a expense budget for year: ' + GetBudgetYears() + '</div>\r\n'
  html += '<br/>\r\n'
  html += '<input id="txtBudgetAction" name="txtBudgetAction" class="textbox" type="hidden" value="" />\r\n'
  html += GetBudgetByCategoriesActionButtons("1")
  html += '<div class="Content">\r\n'
  html += '<table cellpadding="2" cellspacing="2">\r\n'
  html += '<tr>\r\n'
  html += '<td class="ExpHeaderDel">&nbsp;</td>\r\n'
  html += '<td class="ExpHeaderText">Expense Category</td>\r\n'
  html += '<td class="ExpHeaderNum">Total(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jan(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Feb(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Mar(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Apr(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">May(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jun(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Jul(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Aug(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Sep(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Oct(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Nov(b)</td>\r\n'
  html += '<td class="ExpHeaderNum">Dec(b)</td>\r\n'
  html += '</tr>\r\n'
  html += '<tr>\r\n'
  html += '<td class="ExpHeaderDel">&nbsp;</td>\r\n'
  html += '<td class="ExpHeaderText">Total</td>\r\n'
  html += '<td class="ExpDataTotalNum"><--TOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JANTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--FEBTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MARTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--APRTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--MAYTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JUNTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--JULTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--AUGTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--SEPTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--OCTTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--NOVTOTAL--></td>\r\n'
  html += '<td class="ExpDataTotalNum"><--DECTOTAL--></td>\r\n'
  html += '</tr>\r\n'

  html += '<tr>\r\n'
  html += '<td class="ExpDataDel">&nbsp;</td>\r\n'
  html += '<td><--DDLCATEGORY--></td>\r\n'
  html += '<td class="ExpDataNum">&nbsp;</td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtJanNewB" name="txtJanNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtFebNewB" name="txtFebNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtMarNewB" name="txtMarNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtAprNewB" name="txtAprNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtMayNewB" name="txtMayNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtJunNewB" name="txtJunNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtJulNewB" name="txtJulNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtAugNewB" name="txtAugNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtSepNewB" name="txtSepNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtOctNewB" name="txtOctNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtNovNewB" name="txtNovNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '<td class="ExpDataNum"><input id="txtDecNewB" name="txtDecNewB" class="textbox" type="text" value="" /></td>\r\n'
  html += '</tr>\r\n'

  selectedCategories = ''
  janTotal = 0
  febTotal = 0
  marTotal = 0
  aprTotal = 0
  mayTotal = 0
  junTotal = 0
  julTotal = 0
  augTotal = 0
  sepTotal = 0
  octTotal = 0
  novTotal = 0
  decTotal = 0
  total = 0
  totalCat = 0
  index = 0

  budgetDao = BudgetDao()
  
  budgets = budgetDao.GetBudgetExpByUserYear(user.key(), pSelectedYear)

  for budget in budgets:
    if selectedCategories == '':
      selectedCategories += budget.Category.CategoryName.lower()
    else:
      selectedCategories += ',' + budget.Category.CategoryName.lower()


    index += 1
    janTotal += budget.JanExp
    febTotal += budget.FebExp
    marTotal += budget.MarExp
    aprTotal += budget.AprExp
    mayTotal += budget.MayExp
    junTotal += budget.JunExp
    julTotal += budget.JulExp
    augTotal += budget.AugExp
    sepTotal += budget.SepExp
    octTotal += budget.OctExp
    novTotal += budget.NovExp
    decTotal += budget.DecExp

    totalCat = budget.JanExp + budget.FebExp + budget.MarExp + budget.AprExp + budget.MayExp + budget.JunExp + budget.JulExp
    totalCat += budget.AugExp + budget.SepExp + budget.OctExp + budget.NovExp + budget.DecExp

    total += totalCat

    html += '<tr>\r\n'
    html += '<td class="ExpDataDel"><a href="">X</a></td>\r\n'
    html += '<td class="ExpDataCat">\r\n';
    html += '<input id="txtExpCatName' + str(index) + '" name="txtExpCatName' + str(index) + '" class="textbox" type="hidden" value="' + budget.Category.CategoryName.lower() + '" />\r\n'
    html += '<input id="txtBudgetCatKey' + str(index) + '" name="txtBudgetCatKey' + str(index) + '" class="textbox" type="hidden" value="' + str(budget.key()) + '" />\r\n'
    html += budget.Category.CategoryName.upper() + '</td>\r\n'
    html += '<td class="ExpDataNum">' + str(long(round(totalCat,0))) + '</td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtJanB' + str(index) + '" name="txtJanB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.JanExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtFebB' + str(index) + '" name="txtFebB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.FebExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtMarB' + str(index) + '" name="txtMarB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.MarExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtAprB' + str(index) + '" name="txtAprB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.AprExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtMayB' + str(index) + '" name="txtMayB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.MayExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtJunB' + str(index) + '" name="txtJunB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.JunExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtJulB' + str(index) + '" name="txtJulB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.JulExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtAugB' + str(index) + '" name="txtAugB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.AugExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtSepB' + str(index) + '" name="txtSepB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.SepExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtOctB' + str(index) + '" name="txtOctB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.OctExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtNovB' + str(index) + '" name="txtNovB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.NovExp,0))) + '" /></td>\r\n'
    html += '<td class="ExpDataNum"><input id="txtDecB' + str(index) + '" name="txtDecB' + str(index) + '" class="textbox" type="text" value="' + str(long(round(budget.DecExp,0))) + '" /></td>\r\n'
    html += '</tr>\r\n'
    
  html += '</table>\r\n'
  html += '</div>\r\n'
  html += '<input id="txtBudgetIndex" name="txtBudgetIndex" class="textbox" type="hidden" value="' + str(index) + '" />\r\n'
  html += GetBudgetByCategoriesActionButtons("2")
  html = html.replace('<--DDLCATEGORY-->', GetBudgetExpenseCategoriesNotUsed(selectedCategories))
  html = html.replace('<--TOTAL-->', str(long(round(total,0)))).replace('<--JANTOTAL-->', str(long(round(janTotal,0))))
  html = html.replace('<--FEBTOTAL-->', str(long(round(febTotal,0)))).replace('<--MARTOTAL-->', str(long(round(marTotal,0))))
  html = html.replace('<--APRTOTAL-->', str(long(round(aprTotal,0)))).replace('<--MAYTOTAL-->', str(long(round(mayTotal,0))))
  html = html.replace('<--JUNTOTAL-->', str(long(round(junTotal,0)))).replace('<--JULTOTAL-->', str(long(round(julTotal,0))))
  html = html.replace('<--AUGTOTAL-->', str(long(round(augTotal,0)))).replace('<--SEPTOTAL-->', str(long(round(sepTotal,0))))
  html = html.replace('<--OCTTOTAL-->', str(long(round(octTotal,0)))).replace('<--NOVTOTAL-->', str(long(round(novTotal,0))))
  html = html.replace('<--DECTOTAL-->', str(long(round(decTotal,0))))
  
  return html


class BudgetDao():

  def GetBudgetExpByUserYear(self, pUserKey, pYear):
    budgetExpCategories = models.BudgetExpenseByCategory.gql("WHERE User = :1 AND Year = :2 ORDER BY Seq ASC", pUserKey, pYear)
    return budgetExpCategories
  
  def doAddBudgetExpenseByCategory(self, pUserKey, pYear, pCatKey, pSeq, pJanExp, pFebExp, pMarExp, pAprExp, pMayExp, pJunExp, pJulExp,
                                   pAugExp, pSepExp, pOctExp, pNovExp, pDecExp, pLocked):
    budgetExpCategory = models.BudgetExpenseByCategory(User=pUserKey, Year=pYear, Category=pCatKey, Seq=pSeq,
                                           JanExp=pJanExp, FebExp=pFebExp, MarExp=pMarExp, AprExp=pAprExp, MayExp=pMayExp, JunExp=pJunExp, JulExp=pJulExp,
                                           AugExp=pAugExp, SepExp=pSepExp, OctExp=pOctExp, NovExp=pNovExp, DecExp=pDecExp,
                                           Locked=pLocked)
    budgetExpCategory.put()
    return budgetExpCategory

  def doUpdateBudgetExpenseByCategory(self, pBudgetExpByCatKey, pJanExp, pFebExp, pMarExp, pAprExp, pMayExp, pJunExp, pJulExp,
                                      pAugExp, pSepExp, pOctExp, pNovExp, pDecExp):

    budgetExpCategory = db.get(pBudgetExpByCatKey)
    budgetExpCategory.JanExp = pJanExp
    budgetExpCategory.FebExp = pFebExp
    budgetExpCategory.MarExp = pMarExp
    budgetExpCategory.MayExp = pMayExp
    budgetExpCategory.JunExp = pJunExp
    budgetExpCategory.JulExp = pJulExp
    budgetExpCategory.AugExp = pAugExp
    budgetExpCategory.SepExp = pSepExp
    budgetExpCategory.OctExp = pOctExp
    budgetExpCategory.NovExp = pNovExp
    budgetExpCategory.DecExp = pDecExp
    budgetExpCategory.put()
    return budgetExpCategory
